import pygame
from pygame import Surface


class Alien(pygame.sprite.Sprite):
    INIT_POSITION_X = 90  # x轴初始位置
    INIT_POSITION_Y = 550  # y轴初始位置
    MOVE_SPEED = 10  # 移动速度
    FRAME_SPEED = 0.2  # 帧速度
    GRAVITY = 0  # 重力

    def __init__(self):
        super().__init__()
        self.player_pos = pygame.Vector2(self.INIT_POSITION_X, self.INIT_POSITION_Y) # 玩家位置
        self.frame_index = 0

        self.image = self.stand()
        self.rect = self.image.get_rect(topleft=self.player_pos)
    def update(self):
        self.player_input()
    def walk(self) -> list[Surface]:
        # 行走动作
        player_walk_pics = [
                pygame.image.load("pics/player/alienBeige_walk1.png"),
                pygame.image.load("pics/player/alienBeige_walk2.png")
            ]
        
        self.frame_index += self.FRAME_SPEED
        if self.frame_index >= 2:
            self.frame_index = 0
        return player_walk_pics[int(self.frame_index)]
        
    def stand(self) -> Surface:
        # 站立动作
        player_stand_pic = pygame.image.load("pics/player/alienBeige_stand.png")
        return player_stand_pic

    def jump(self) -> Surface:
        # 跳跃动作
        player_jump_pic = pygame.image.load("pics/player/alienBeige_jump.png")
        return player_jump_pic

    def player_input(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            self.rect.x = max(0, self.rect.x - self.MOVE_SPEED)
            self.image = self.walk()

        if keys[pygame.K_d]:
            self.rect.x = min(920, self.rect.x + self.MOVE_SPEED)
            self.image = self.walk()

        if keys[pygame.K_SPACE]:  # 跳跃
            if self.rect.y == 550:
                self.GRAVITY = -20
                self.image = self.jump()

        # 重力，用于模拟玩家跳跃
        self.GRAVITY += 1
        self.rect.y += self.GRAVITY

        if self.rect.y > 550:
            self.rect.y = 550
            self.GRAVITY = 0

        if self.rect.y < 550:
            self.image = self.jump()

        if not keys[pygame.K_a] and not keys[pygame.K_d] and self.rect.y == 550:
            self.image = self.stand()